/** 
 * Description:用户积分查询
 * Date:2011-11-15
 * Author:Along	
*/
package com.community.shopping.web;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.community.shopping.domain.Order;
import com.community.shopping.domain.OrderItem;
import com.community.shopping.domain.User;
import com.community.shopping.domain.UserScore;
import com.community.shopping.service.logic.OrderManager;
import com.community.shopping.service.logic.Page;
import com.community.shopping.service.logic.UserManager;
import com.community.shopping.service.logic.UserScoreManager;


@Controller 
public class UserScoreControl {

	@Autowired  
    private UserScoreManager userScoreManager;
	@Autowired  
    private UserManager userManager;
	private SimpleDateFormat formatDate=new SimpleDateFormat("yyyy-MM-dd");
	private SimpleDateFormat formatMonth=new SimpleDateFormat("yyyy-MM");
	private SimpleDateFormat formatNum=new SimpleDateFormat("yyyyMMdd");
	
	
	/**
	 * 查询积分详情
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/query/userscore")
	public void queryUserScore(HttpServletRequest request,HttpServletResponse response)throws Exception
	{
		String userId=request.getParameter("userId");
		User user=userManager.getUserById(userId);
		String time=request.getParameter("time");
		String filter=" userId="+userId;
		Page page=new Page(request,8);		
		List<UserScore> UserScores=userScoreManager.getUserScores(filter, page);
		StringBuilder sb=new StringBuilder("{\"userscores\":[");
		for(UserScore userScore:UserScores)
		{
			if(sb.length()>20)
			{
				sb.append(",");	
			}
			sb.append("{");
			sb.append("\"orderid\":\""+userScore.getOrderId()+"\",");
			sb.append("\"no\":\""+userScore.getOrderNo()+"\",");
			sb.append("\"score\":\""+userScore.getScore()+"\",");
			sb.append("\"createdate\":\""+userScore.getCreateDate()+"\",");
			sb.append("\"totalscore\":\""+userScore.getScoreResult()+"\"");
			sb.append("}");		
		}
		sb.append("],\"pageno\":\""+page.getCurrentPage()+"\",\"totalpage\":\""+page.getTotalPages()
				+"\",\"totalsize\":\""+page.getTotalRowsAmount()+"\",\"score\":\""+user.getScore()+"\"}");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().print(sb.toString()); 			
	}


}
